import { Model, DataTypes, Sequelize, Optional } from 'sequelize';
import User from './User';

interface InterestTagAttributes {
    id: string;
    name: string;
    category: string;
    description?: string | null;
    icon?: string | null;
    createdAt?: Date;
    updatedAt?: Date;
}

interface InterestTagCreationAttributes extends Optional<InterestTagAttributes, 'id' | 'createdAt' | 'updatedAt' | 'description' | 'icon'> { }

export class InterestTag extends Model<InterestTagAttributes, InterestTagCreationAttributes> implements InterestTagAttributes {
    public id!: string;
    public name!: string;
    public category!: string;
    public description?: string | null;
    public icon?: string | null;
    public readonly createdAt!: Date;
    public readonly updatedAt!: Date;

    public static associate(models: {
        User: typeof User;
    }) {
        InterestTag.belongsToMany(models.User, {
            through: 'UserInterestTag',
            foreignKey: 'tagId',
            otherKey: 'userId',
            as: 'users'
        });
    }
}

export const initInterestTag = (sequelize: Sequelize) => {
    InterestTag.init(
        {
            id: {
                type: DataTypes.UUID,
                defaultValue: DataTypes.UUIDV4,
                primaryKey: true,
            },
            name: {
                type: DataTypes.STRING(50),
                allowNull: false,
                unique: true,
            },
            category: {
                type: DataTypes.STRING(50),
                allowNull: false,
            },
            description: {
                type: DataTypes.STRING(200),
                allowNull: true,
            },
            icon: {
                type: DataTypes.STRING(200),
                allowNull: true,
            },
        },
        {
            sequelize,
            tableName: 'interest_tags',
            timestamps: true,
        }
    );
};

export default InterestTag; 